package com.iemes.controller.finance;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.PcontractTrackingDetailFormMap;
import com.iemes.entity.PcontractTrackingFormMap;
import com.iemes.mapper.finance.PcontractTrackingDetailMapper;
import com.iemes.mapper.finance.PcontractTrackingMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/finance")
@Controller
public class PcontractTrackingController extends BaseController {
	
	@Inject
	private PcontractTrackingMapper pcontractTrackingMapper;
	
	@Inject
	private PcontractTrackingDetailMapper pcontractTrackingDetailMapper;

	@RequestMapping("pcontractTracking")
	public String pcontractTracking(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/pcontractTracking_list";
	}
	
	
	@ResponseBody
	@RequestMapping("pcontractTracking_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		PcontractTrackingFormMap pcontractTrackingFormMap=getFormMap(PcontractTrackingFormMap.class);
		List<Map<String,Object>> pcontractTrackingFormMaps=pcontractTrackingMapper.getPcontractTrackingByCondition(pcontractTrackingFormMap);
		LuogicPaging(pageNow, pageSize, pcontractTrackingFormMaps);
		pageView.setRecords(pcontractTrackingFormMaps);
		return pageView;
	}
	
	
	@RequestMapping("pcontractTracking_addUI")
	public String addUI(Model model) throws Exception {
		List<BrandFormMap> brandFormMap=baseMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brandFormMap", brandFormMap);
		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/pcontractTracking_add";
	}
	
	
	@ResponseBody
	@RequestMapping("pcontractTracking_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="采购合同跟踪",methods="原料-记录来料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		
		//采购合同父项
		PcontractTrackingFormMap pcontractTrackingFormMap = getFormMap(PcontractTrackingFormMap.class);
		pcontractTrackingFormMap.put("state", 0);
		pcontractTrackingMapper.addEntity(pcontractTrackingFormMap);
		
		//采购合同子项
		int pId=pcontractTrackingMapper.getMaxId();
		//int pId=Integer.parseInt(request.getParameter("pcontractTrackingFormMap.id"));
		String[] itemid = request.getParameterValues("item_no_1");
		String[] itemUseNumberArr = request.getParameterValues("item_time");
		String[] itemBalanceUpArr = request.getParameterValues("item_balance_up_1");
		String[] itemBalanceDownArr = request.getParameterValues("item_balance_down_1");
		String[] itemBalanceDownArr2 = request.getParameterValues("item_balance_sit_1");
		if(null != itemid ){
			for(int i=0;i<itemid.length;i++){
			PcontractTrackingDetailFormMap pcontractTrackingDetailFormMap=new PcontractTrackingDetailFormMap();
			pcontractTrackingDetailFormMap.put("pcontractTrackingid", pId);
			pcontractTrackingDetailFormMap.put("incomingBatches", itemid[i]);
			pcontractTrackingDetailFormMap.put("deliveryDate", itemUseNumberArr[i]);
			pcontractTrackingDetailFormMap.put("rawMaterialModel", itemBalanceUpArr[i]);
			pcontractTrackingDetailFormMap.put("consignNum", itemBalanceDownArr[i]);
			pcontractTrackingDetailFormMap.put("selectSum", Double.parseDouble(itemBalanceDownArr2[i]));
			pcontractTrackingDetailFormMap.put("state", 0);
			baseMapper.addEntity(pcontractTrackingDetailFormMap);
			}
		}
		
		return "success";
	}
	
	
	@RequestMapping("pcontractTracking_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		//获取物料对象
		PcontractTrackingFormMap pcontractTrackingFormMap = pcontractTrackingMapper.getPcontractTrackingById(Integer.parseInt(id));
		model.addAttribute("pcontractTracking", pcontractTrackingFormMap);
		
		//显示供应商
		List<BrandFormMap> brandFormMap=baseMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brandFormMap", brandFormMap);
		
		//显示子项
		List<PcontractTrackingDetailFormMap> pcontractTrackingDetail=pcontractTrackingDetailMapper
				.getPcontractTrackingDetail(pcontractTrackingFormMap.getInt("id"));
		
		model.addAttribute("pcontractTrackingDetail", pcontractTrackingDetail);
		
		
		
		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/pcontractTracking_edit";
	}
	
	
	
	@ResponseBody
	@RequestMapping(value="pcontractTracking_editEntity")
	@SystemLog(module="采购合同跟踪",methods="合同-修改原料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		PcontractTrackingFormMap pcontractTrackingFormMap = getFormMap(PcontractTrackingFormMap.class);
		pcontractTrackingMapper.editEntity(pcontractTrackingFormMap);
		
		//先删除
		baseMapper.deleteByAttribute("pcontractTrackingid","'"+ Integer.parseInt(pcontractTrackingFormMap.getStr("id")) +"'", PcontractTrackingDetailFormMap.class);
		System.out.println("id"+Integer.parseInt(pcontractTrackingFormMap.getStr("id")));
		System.out.println("合同编号"+pcontractTrackingFormMap.getStr("contractNum"));
		System.out.println("原料厂商id"+Integer.parseInt(pcontractTrackingFormMap.getStr("materialBrandId")));
		
		//采购合同子项
		//int pId=pcontractTrackingMapper.getMaxId();
		String[] itemid = request.getParameterValues("item_no_1");
		String[] itemUseNumberArr = request.getParameterValues("item_time");
		String[] itemBalanceUpArr = request.getParameterValues("item_balance_up_1");
		String[] itemBalanceDownArr = request.getParameterValues("item_balance_down_1");
		String[] itemBalanceDownArr2 = request.getParameterValues("item_balance_sit_1");
		if(null != itemid ){
			for(int i=0;i<itemid.length;i++){
				PcontractTrackingDetailFormMap pcontractTrackingDetailFormMap=new PcontractTrackingDetailFormMap();
				pcontractTrackingDetailFormMap.put("pcontractTrackingid", Integer.parseInt(pcontractTrackingFormMap.getStr("id")));
				pcontractTrackingDetailFormMap.put("incomingBatches", itemid[i]);
				pcontractTrackingDetailFormMap.put("deliveryDate", itemUseNumberArr[i]);
				pcontractTrackingDetailFormMap.put("rawMaterialModel", itemBalanceUpArr[i]);
				pcontractTrackingDetailFormMap.put("consignNum", itemBalanceDownArr[i]);
				pcontractTrackingDetailFormMap.put("selectSum", Double.parseDouble(itemBalanceDownArr2[i]));
				pcontractTrackingDetailFormMap.put("state", 0);
				baseMapper.addEntity(pcontractTrackingDetailFormMap);
			}
		
		}
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value="pcontractTracking_deleteEntity")
	@SystemLog(module="采购合同跟踪",methods="合同-删除原料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		
		PcontractTrackingFormMap pcontractTrackingFormMap=getFormMap(PcontractTrackingFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		
		for(int i=0;i<str.length;i++){
			System.out.println("循环打印"+str[i]+"呱呱");
			pcontractTrackingFormMap.set("state", 1);
			pcontractTrackingFormMap.put("id", str[i]);
			pcontractTrackingMapper.editEntity(pcontractTrackingFormMap);
			
			List<PcontractTrackingDetailFormMap> pcontractTrackingDetail=pcontractTrackingDetailMapper
					.getPcontractTrackingDetail(Integer.parseInt(pcontractTrackingFormMap.getStr("id")));
			
			PcontractTrackingDetailFormMap pcontractTrackingDetailFormMap=getFormMap(PcontractTrackingDetailFormMap.class);
			for(PcontractTrackingDetailFormMap p:pcontractTrackingDetail){
				pcontractTrackingDetailFormMap.set("state", 1);
				pcontractTrackingDetailFormMap.put("id", p.getInt("id"));
				pcontractTrackingDetailMapper.editEntity(pcontractTrackingDetailFormMap);
			}
		}
		
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value="pcontractTrackingDetail_deleteEntity")
	@SystemLog(module="采购合同跟踪",methods="合同-删除原料子项")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deletepcontractTrackingDetailEntity() throws Exception {
		
		String id = getPara("pid");
		System.out.println("你的id"+id);
		PcontractTrackingDetailFormMap pcontractTrackingDetailFormMap=getFormMap(PcontractTrackingDetailFormMap.class);
		pcontractTrackingDetailFormMap.set("state", 1);
		pcontractTrackingDetailFormMap.put("id", id);
		pcontractTrackingDetailMapper.editEntity(pcontractTrackingDetailFormMap);
		return "success";
	}
	
	
}
